﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Odelle;
using Odelle.Utils;
using Odelle.Utils.Comparisons;

namespace Test
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        Odelle.Utils.Comparisons.XmlQuery xquery = new Odelle.Utils.Comparisons.XmlQuery();
        System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (txtSource.Text != "")
            {
                try
                {
                    xmldoc.LoadXml(txtSource.Text);
                    updateNames(xmldoc.DocumentElement, ref txtName1);
                }
                catch (Exception)
                {
                }

            }
        }

        private void btnFetchCharacter_Click(object sender, EventArgs e)
        {
            Odelle.WoWArmory.AccessArmory armory = new Odelle.WoWArmory.AccessArmory();
            string html = armory.FetchArmoryCharacter(cmbRealm.Text, cmbCharacter.Text);
            txtSource.Text = html;
        }

        private void btnFetchGuild_Click(object sender, EventArgs e)
        {
            Odelle.WoWArmory.AccessArmory armory = new Odelle.WoWArmory.AccessArmory();
            string html = armory.FetchArmoryGuild(cmbRealm.Text, cmbGuild.Text,int.Parse(txtPage.Text));
            txtSource.Text = html;
        }

        private void txtName1_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateValues(xmldoc.DocumentElement, ref txtName1, ref txtValue1);
        }

        private void txtName2_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            updateValues(ele, ref txtName2, ref txtValue2);
        }

        private void txtName3_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            updateValues(ele2, ref txtName3, ref txtValue3);
        }

        private void txtName4_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            updateValues(ele3, ref txtName4, ref txtValue4);
        }

        private void txtName1_TextChanged(object sender, EventArgs e)
        {
            updateValues(xmldoc.DocumentElement, ref txtName1, ref txtValue1);
        }

        private void txtName2_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            updateValues(ele, ref txtName2, ref txtValue2);
        }

        private void txtName3_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            updateValues(ele2, ref txtName3, ref txtValue3);
        }

        private void txtName4_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            updateValues(ele3, ref txtName4, ref txtValue4);
        }

        private void updateValues(XmlElement element, ref ComboBox names, ref ComboBox values)
        {
            values.Items.Clear();

            if (names.Text == "")
                return;

            string[] list = xquery.GetAttributeValues(element, names.Text);
            for (int i = 0; i < list.Length; ++i)
            {
                values.Items.Add(list[i]);
            }
        }

        private void txtSource_TextChanged(object sender, EventArgs e)
        {
            try
            {
                xmldoc.LoadXml(txtSource.Text);
                updateNames(xmldoc.DocumentElement, ref txtName1);
            }
            catch (Exception)
            {
            }
        }

        private void updateNames(XmlElement element, ref ComboBox names)
        {
            string[] list = xquery.GetAttributeNames(element);
            names.Items.Clear();

            for (int i = 0; i < list.Length; ++i)
            {
                names.Items.Add(list[i]);
            }
        }

        private void txtValue1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            updateNames(ele, ref txtName2);

            if (ele != null)
                txtResult.Text = ele.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele);
            setXmlQuery();
        }

        private void txtValue2_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            updateNames(ele2, ref txtName3);

            if (ele2 != null)
                txtResult.Text = ele2.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele2);
            setXmlQuery();
        }

        private void txtValue3_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            updateNames(ele3, ref txtName4);

            if (ele3 != null)
                txtResult.Text = ele3.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele3);
            setXmlQuery();
        }

        private void txtValue4_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            XmlElement ele4 = xquery.QueryXmlSingleElement(ele3, txtName4.Text, txtValue4.Text);

            if (ele4 != null)
                txtResult.Text = ele4.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele4);
            setXmlQuery();
        }

        private void txtValue1_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            updateNames(ele, ref txtName2);

            if (ele != null)
                txtResult.Text = ele.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele);
            setXmlQuery();
        }

        private void txtValue2_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            updateNames(ele2, ref txtName3);

            if (ele2 != null)
                txtResult.Text = ele2.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele2);
            setXmlQuery();
        }

        private void txtValue3_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            updateNames(ele3, ref txtName4);

            if (ele3 != null)
                txtResult.Text = ele3.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele3);
            setXmlQuery();
        }

        private void txtValue4_TextChanged(object sender, EventArgs e)
        {
            XmlElement ele = xquery.QueryXmlSingleElement(xmldoc.DocumentElement, txtName1.Text, txtValue1.Text);
            XmlElement ele2 = xquery.QueryXmlSingleElement(ele, txtName2.Text, txtValue2.Text);
            XmlElement ele3 = xquery.QueryXmlSingleElement(ele2, txtName3.Text, txtValue3.Text);
            XmlElement ele4 = xquery.QueryXmlSingleElement(ele3, txtName4.Text, txtValue4.Text);

            if (ele4 != null)
                txtResult.Text = ele4.OuterXml;
            txtInnerText.Text = Odelle.Utils.DhtmlHandler.GetInnerText(ele4);
        }

        private void setXmlQuery()
        {
            string query = "";

            if (txtName1.Text != "")
                query = query + "@\"" + txtName1.Text + "\\" + txtValue1.Text + "\"";
            if (txtName2.Text != "")
                query = query + ", @\"" + txtName2.Text + "\\" + txtValue2.Text + "\"";
            if (txtName3.Text != "")
                query = query + ", @\"" + txtName3.Text + "\\" + txtValue3.Text + "\"";
            if (txtName4.Text != "")
                query = query + ", @\"" + txtName4.Text + "\\" + txtValue4.Text + "\"";
            if (query != "")
                query = "new string[] { " + query + "}";

            txtXmlQuery.Text = query;
        }

        private void btnClearSource_Click(object sender, EventArgs e)
        {
            txtSource.Text = "";
        }
    }
}
